﻿/*
   Copyright 2013 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace Flower.Actors.Tests
{
    [TestFixture]
    public class ArrayHeapTests
    {
        public class Item
        {
            public int Priority { get; set; }
        }

        public class ItemComparer : IComparer<Item>
        {
            public int Compare(Item x, Item y)
            {
                return x.Priority - y.Priority;
            }
        }

        static void CheckHeapness(ArrayHeap<Item> heap)
        {
            for (int i = 1; i < heap.Count; i++)
            {
                int parentIdx = ArrayHeap<Item>.ParentIndex(i);

                Assert.IsFalse(heap[i].Priority < heap[parentIdx].Priority);
            }
        }

        static void CheckChildren(ArrayHeap<Item> heap)
        {
            for (int i = 1; i < heap.Count; i++)
            {
                int parentIdx = ArrayHeap<Item>.ParentIndex(i);
                int lchildIdx = ArrayHeap<Item>.LeftChildIndex(parentIdx);
                int rchildIdx = lchildIdx + 1;

                Assert.IsTrue(i == lchildIdx || i == rchildIdx);
                Assert.IsTrue(ArrayHeap<Item>.ParentIndex(lchildIdx) == parentIdx);
                Assert.IsTrue(ArrayHeap<Item>.ParentIndex(rchildIdx) == parentIdx);
            }
        }

        [Test]
        public void AddPopTest()
        {
            var heap = new ArrayHeap<Item>(10, new ItemComparer());

            var rng = new Random();
            const int nTests = 100;

            for (int i = 0; i < nTests; i++)
            {
                heap.Add(new Item { Priority = rng.Next(0, 10) });

                CheckHeapness(heap);
            }

            CheckChildren(heap);

            for (int i = 0; i < nTests; i++)
            {
                heap.Pop();

                CheckHeapness(heap);
            }
        }

        [Test]
        public void ModificationOfEnumeratedCollection()
        {
            var heap = new ArrayHeap<Item>(10, new ItemComparer());

            const int nTests = 10;
            var rng = new Random();

            for (int i = 0; i < nTests; i++)
            {
                heap.Add(new Item { Priority = rng.Next(0, 10) });
            }

            try
            {
                foreach (Item item in heap)
                {
                    heap.Pop();
                }

                Assert.Fail();
            }
            catch (InvalidOperationException) { }
        }
    }
}
